package org.aersoft.jokebook.fragments;

import java.util.ArrayList;

import org.aersoft.jokebook.R;
import org.aersoft.jokebook.activities.ItemActivity;
import org.aersoft.jokebook.adapters.ItemListAdapter;
import org.aersoft.jokebook.beans.ItemBean;
import org.aersoft.jokebook.core.Constants;
import org.aersoft.jokebook.dao.AppDAO;
import org.aersoft.jokebook.dao.api.IAppDAO;

import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class CategoryFragment extends ListFragment {

	private int categoryId;

	/**
	 * Constructor method of class.
	 */
	public CategoryFragment() {}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		categoryId = getActivity().getIntent().getExtras().getInt(Constants.CATEGORY);		
		IAppDAO appDAO = new AppDAO().getContext(getActivity());
		ArrayList<ItemBean> listItems = appDAO.getJokeListByCategory(categoryId);
		appDAO.close();
		if (listItems != null && listItems.size() > 0) getActivity().setTitle(listItems.get(0).getCategoryName());
		setListAdapter(new ItemListAdapter(getActivity(), R.layout.list_joke_by_category, listItems));
	}

	@Override
	public void onListItemClick(ListView listView, View view, int position, long id) {
		super.onListItemClick(listView, view, position, id);
		Intent intent = new Intent(getActivity(), ItemActivity.class);
		intent.putExtra(Constants.CATEGORY, categoryId).putExtra(Constants.ID, position);
		startActivity(intent);
	}
}
